/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

<template>
  <div class='music-detail'>
    <div 
      class='nav' 
      :style='{ marginTop: `${ store.state.topAvoidArea }px` }'
    >
      <DetailNav 
        :deviceType='deviceType' 
        :musicId='musicId'
      ></DetailNav>
    </div>
    <div class='content'>
      <div :class="['play-page', isMobileOrPortrait ? 'play-page-mobile' : '']">
        <div
          v-show='noMobileOrTable'
          :class="[
            'artwork-icon',
            isMobileOrLandscape ? 'mobile-icon' : 'pc-icon',
          ]"
        >
          <TransformImg></TransformImg>
        </div>
        <div class='tablet-music-info' v-show='isTabletPortrait'>
          <div class='tablet-music-name'>
            <img src='../../assets/images/artwork.svg' alt='' />
            <div class='name'>
              <span>{{ oneMusic?.name }}</span>
              <span>{{ oneMusic?.musicName }}</span>
            </div>
          </div>
          <img src='../../assets/images/love_count.svg' alt='' />
          <img src='../../assets/images/addNumber.svg' alt='' />
        </div>
        <div class='lyric-page' v-show='isTabletPortrait'>
          <span>此歌曲为纯音乐，请您欣赏</span>
        </div>
        <MusicInfo
          :oneMusic='oneMusic'
        ></MusicInfo>
        <div class='play'>
          <MusicPlayer
            :musicId='musicId'
            :isPlay='isPlay'
            @updateMusic='updateMusic'
            @updateIsPlay='updateIsPlay'
          ></MusicPlayer>
        </div>
      </div>
      <div
        :class="[
          'lyric-page',
          isMobileOrPortrait ? 'lyric-page-mobile' : 'lyric-page-pc',
        ]"
      >
        <span>此歌曲为纯音乐，请您欣赏</span>
      </div>
    </div>
  </div>
</template>

<script lang='ts' setup>
import { ref, onMounted, onBeforeUnmount, computed } from 'vue';
import { useStore } from 'vuex';
import { musicListType } from '../../utils/common';
import { useRoute } from 'vue-router';
import DetailNav from '../../components/DetailNav/detailNav.vue';
import MusicPlayer from '../../components/Player/musicPlayer.vue';
import MusicInfo from '../../components/MusicInfo/musicInfo.vue';
import TransformImg from '../../components/TransformImg/transformImg.vue';

const route = useRoute();
const store = useStore();
const musicList = ref<musicListType[]>(store.state.musicList);
const isPlay = ref<number>(Number(route.query.isPlay));
const musicId = ref<number>(Number(route.query.musicId));
const oneMusic = ref<object>({});
const isMobile = ref<boolean>(true);
const deviceType = ref<string>('mobile');
const currentOrientation = ref<string>('landscape');
const isMobileOrPortrait = computed(
  () =>
    deviceType.value === 'mobile' ||
    (deviceType.value === 'tablet' && currentOrientation.value === 'portrait')
);
const isTabletPortrait = computed(
  () => deviceType.value === 'tablet' && currentOrientation.value === 'portrait'
);
const isMobileOrLandscape = computed(
  () =>
    deviceType.value === 'mobile' ||
    (deviceType.value === 'tablet' && currentOrientation.value !== 'portrait')
);
const noMobileOrTable = computed(
  () =>
    deviceType.value !== 'tablet' ||
    (deviceType.value === 'tablet' && currentOrientation.value !== 'portrait')
);
const updateMusic = (num: number): void => {
  oneMusic.value = musicList.value[num - 1];
  musicId.value = num;
};
const updateIsPlay = (num: number) => {
  isPlay.value = num;
};

const updateScreenSize = () => {
  if (window.innerWidth < 500 && window.innerHeight < 1000) {
    deviceType.value = 'mobile';
    isMobile.value = true;
  } else if (
    window.innerWidth > 500 &&
    window.innerWidth < 1000 &&
    window.innerHeight < 1000
  ) {
    deviceType.value = 'foldableScreen';
  } else if (window.innerWidth > 1350) {
    deviceType.value = 'pc';
    isMobile.value = false;
  } else {
    isMobile.value = false;
    if (screen.orientation.type.indexOf('landscape') >= 0) {
      currentOrientation.value = 'landscape';
    } else {
      currentOrientation.value = 'portrait';
    }
    deviceType.value = 'tablet';
  }
};

onMounted(() => {
  musicList.value = store.state.musicList;
  oneMusic.value = musicList.value.find((item) => item.id === musicId.value);
  isMobile.value = window.innerWidth < 500;
  updateScreenSize();
  window.addEventListener('resize', updateScreenSize);
});

onBeforeUnmount(() => {
  window.removeEventListener('resize', updateScreenSize);
});
</script>

<style lang='less' scoped>
@import './index.less';
</style>
